/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.living.filedownloader.ui.audiosel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloader;
import com.living.R;
import com.living.filedownloader.DownLoadManger;
import com.living.filedownloader.ui.adapter.TaskItemViewHolder;
import com.living.filedownloader.GlobalTasksManager;
import com.living.widget.BottomMenuView;
import com.md.livingwidget.CoreBaseActivity;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.Map;

/**
 * 选择下载的页面
 * Created by wangq
 */
public class TasksSelectActivity extends CoreBaseActivity {


    private BottomMenuView bottom_menu;
    private AudioSelFragment audioBatchFragment;
//    private Album album;


    public static void intentTo(Context context, Album album) {
        Intent intent = new Intent(context, TasksSelectActivity.class);
        intent.putExtra("album", album);
//        intent.putExtra("audio", audio);
        context.startActivity(intent);
    }

    public static void intentTo(Context context, ArrayList<Audio> audios) {
        Intent intent = new Intent(context, TasksSelectActivity.class);
        intent.putExtra("audios", audios);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_select_tasks);
        Intent intent = getIntent();
        ArrayList<Audio> downloadedList = new ArrayList<>();
        if (intent.hasExtra("album")) {
            Album album = (Album) intent.getParcelableExtra("album");
            downloadedList.addAll(album.getAudios());
        }
        if (intent.hasExtra("audios")) {
            ArrayList<Audio> list = intent.getParcelableArrayListExtra("audios");
            downloadedList.addAll(list);
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        audioBatchFragment = (AudioSelFragment) fragmentManager.findFragmentById(R.id.fm_batch_delete);

        audioBatchFragment.setDownloadedList(downloadedList);

        bottom_menu = findViewById(R.id.bottom_menu);
        bottom_menu.setMenuShows(false, true, false, false);
        bottom_menu.setMenuDownloadEnable(false);

        audioBatchFragment.setOnBatchSelectLisenter(new AudioSelFragment.OnBatchSelectLisenter() {
            @Override
            public void onSelect(int count, int total) {
                if (count > 0) {
                    bottom_menu.setMenuDownloadEnable(true);
                } else {
                    bottom_menu.setMenuDownloadEnable(false);
                }
            }
        });
        bottom_menu.setOnBottomMenuViewClick(new BottomMenuView.OnBottomMenuViewClick() {
            @Override
            public void onClickAdd(View view) {
                Toast.makeText(getmActivity(), "去李越专辑添加页面", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onClickDownload(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                if (mapSel.size() > 0) {
                    ArrayList<Audio> audioArrayList = new ArrayList<>();
                    //遍历map中的值
                    for (Audio audio : mapSel.values()) {
                        audioArrayList.add(audio);
                    }
                    DownLoadManger.getInstance().downloadAudioList(audioArrayList);
                    Toast.makeText(getmContext(), "添加下载成功", Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    Toast.makeText(getmContext(), "请选择要下载的", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onClickShare(View view) {
                Toast.makeText(getmActivity(), "分享组件", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onClickDelete(View view) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
